import { App, mall, navigate, toast } from '../../../../library/api/index'

Page({
  data: {
    navMe: 'on',
    orderId: '',
    return_default: '请选择',
    reasons: [
      '质量问题',
      '不想要了'
    ],
    index: -1,
    uploads: [],
    upload_urls: [],
    tip: 'blank',
    tipData: {},
    hideTextarea: false
  },
  onLoad: function(e) {
    this.setData({
      orderId: e.order_id
    })
    App.jice.onLoad(e)
  },
  onReady: function() {
    App.jice.track('page', {
      pagepath: '/pages/me/order/return/index',
      pageTitle: '个人中心-订单-退货填写',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '个人中心-订单-退货填写'
    })
  },
  onShow: function() {
    App.jice.track('appshow')
  },
  bindReasonChange: function(e) {
    console.log('bindReasonChange')
    this.setData({
      return_default: '',
      index: e.detail.value
    })
  },
  uploadImage: function() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        var path = res.tempFilePaths[0]
        toast.show(200000, '正在上传')
        wx.uploadFile({
          url: App.G.Server + 'mall/return/upload/',
          filePath: path,
          name: 'image',
          success: (res) => {
            console.log(res)
            toast.close()
            if(res.statusCode == 200) {
              var data = {}
              if(typeof res.data === 'string') data = JSON.parse(res.data.trim())
              this.data.upload_urls.push(data.url)
              this.setData({
                upload_urls: this.data.upload_urls
              })
              console.log(this.data.upload_urls)
            } else {
              this.showErrorTip('上传失败，请重新上传！')
            }
          }
        })
      }
    })
  },
  remove: function(e) {
    let idx = e.currentTarget.dataset.idx
    console.log(idx)
    for(let i = 0; i < this.data.upload_urls.length; i++) {
      if(i == idx) {
        this.data.upload_urls.splice(i, 1)
      }
    }
    this.setData({
      upload_urls: this.data.upload_urls
    })
  },
  submit: function(e) {
    let idx = e.detail.value.reason
    let data = {
      order_id: this.data.orderId,
      form_id: e.detail.formId,
      reason: this.data.reasons[idx],
      desc: e.detail.value.desc,
      uploads: this.data.upload_urls,
    }
    if(idx == -1) {
      this.showErrorTip('提交失败，请选择退货原因以便审核哦！')
      return
    }
    if(idx == 0 && data.desc.length == 0) {
      this.showErrorTip('提交失败，请详细说明您的退货原因以便审核哦！')
      return
    }
    if(idx == 0) {
      if(data.uploads.length == 0) {
        this.showErrorTip('提交失败，质量问题请拍照上传以便审核哦！')
        return
      }
    }
    mall.applyReturn(data, r => {
      toast.close()
      if(r.code == 200) {
        navigate.redirect(`/pages/me/order/list/index`)
      } else {
        this.showErrorTip(r.data)
      }
    }, r => {
      console.log(r)
      this.showErrorTip('系统错误，请稍后再试~')
    })
  },
  showErrorTip: function(message) {
    this.setData({
      hideTextarea: true,
      tip: 'error_tip',
      tipData: message
    })
  },
  closeTip: function() {
    this.setData({
      hideTextarea: false,
      tip: 'blank',
      tipData: {}
    })
  }
})